<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="首页轮播图" prop="carouselUrl">-->
<!--        <el-input-->
<!--          v-model="queryParams.carouselUrl"-->
<!--          placeholder="请输入首页轮播图"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="暂用" prop="carouselActivity">-->
<!--        <el-input-->
<!--          v-model="queryParams.carouselActivity"-->
<!--          placeholder="请输入暂用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分站地区" prop="carouselArea">-->
<!--        <el-input-->
<!--          v-model="queryParams.carouselArea"-->
<!--          placeholder="请输入分站地区"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户ID" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分站ID" prop="substationId">-->
<!--        <el-input-->
<!--          v-model="queryParams.substationId"-->
<!--          placeholder="请输入分站ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fenzhan:carousel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fenzhan:carousel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fenzhan:carousel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fenzhan:carousel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carouselList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="首页轮播ID" align="center" prop="carouselId" />
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="首页轮播图" align="center" prop="carouselUrl" >
        <template slot-scope="scope">
          <el-image :src="scope.row.carouselUrl"></el-image>
        </template>
      </el-table-column>

<!--      <el-table-column label="暂用" align="center" prop="carouselActivity" />-->
      <el-table-column label="分站地区" align="center" prop="carouselArea" />
<!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
<!--      <el-table-column label="分站ID" align="center" prop="substationId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fenzhan:carousel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fenzhan:carousel:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页轮播图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

<!--        <el-form-item label="首页轮播图" prop="carouselUrl">-->
<!--          <imageUpload v-model="form.carouselUrl"/>-->
<!--        </el-form-item>-->

        <el-form-item label="首页轮播图" prop="carouselUrl">
          <!--<el-input v-model="form.galleryInfoPic" placeholder="请输入图片" />-->
          <el-upload
            class="upload-demo"
            :http-request="upload"
            action="aaa"
            accept=".jpg,.png"
            :limit="1"
            :file-list="fileList"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-error="handleError"
            ref="upload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">仅支持jpg,png格式,以最新上传为准</div>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCarousel, getCarousel, delCarousel, addCarousel, updateCarousel, exportCarousel, uploadImage } from "@/api/fenzhan/carousel";
  import ImageUpload from '@/components/ImageUpload';
  export default {
    name: "Carousel",
    components: {
      ImageUpload,
    },
    data() {
      return {
        //图片文件
        fileList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 首页轮播图表格数据
        carouselList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          carouselUrl: null,
          carouselActivity: null,
          carouselArea: null,
          userId: null,
          substationId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          deptId: [
            { required: true, message: "部门ID不能为空", trigger: "blur" }
          ],
          carouselUrl: [
            { required: true, message: "首页轮播图不能为空", trigger: "blur" }
          ],
          carouselArea: [
            { required: true, message: "分站地区不能为空", trigger: "blur" }
          ],
          userId: [
            { required: true, message: "用户ID不能为空", trigger: "blur" }
          ],
          substationId: [
            { required: true, message: "分站ID不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询首页轮播图列表 */
      getList() {
        this.loading = true;
        listCarousel(this.queryParams).then(response => {
          this.carouselList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          carouselId: null,
          deptId: null,
          carouselUrl: null,
          carouselActivity: null,
          carouselArea: null,
          delFlag: null,
          userId: null,
          substationId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.carouselId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.fileList = [];
        this.title = "添加首页轮播图";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const carouselId = row.carouselId || this.ids
        getCarousel(carouselId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改首页轮播图";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.carouselId != null) {
              updateCarousel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCarousel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const carouselIds = row.carouselId || this.ids;
        this.$confirm('是否确认删除首页轮播图编号为"' + carouselIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCarousel(carouselIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有首页轮播图数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCarousel(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },



      //文件上传
      upload(param){
        let file = param.file;
        let fileType = file.type;
        let isLt2M = file.size / 1024 / 1024 < 500;
        if (!isLt2M) {
          this.$message.error("上传大小大小必须小于500M");
          this.fileList=[];
          return;
        }
        let form = new FormData();
        form.append("file", file);
        uploadImage(form).then((res)=>{
          if(res.code==200){
            this.form.carouselUrl = "http://51jhf.com/profile/xiazai"+res.filePath;
          }
          console.log(res);
        })
      },
      //移除文件之前
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      //移除
      handleRemove(file, fileList){
        this.form.galleryInfoPic = '';
        this.fileList=[];
      },
      //上传异常
      handleError(err, file, fileList){
        this.$message.error("上传失败");
      },


    }
  };
</script>
